<template>
  <div>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" :hide-required-asterisk=true>
      <el-form-item label="账号" prop="username">
        <el-input
            v-model="ruleForm.username"
            clearable
            size="default"
            prefix-icon="el-icon-user"
            placeholder="请输入账号"

        ></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="username">
        <el-input
            v-model="ruleForm.username"
            clearable
            size="default"
            prefix-icon="el-icon-user"
            placeholder="请输入账号"

        ></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input
            type="password"
            v-model="ruleForm.password"
            clearable
            size="default"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
            show-password
            autocomplete="off">
        </el-input>
      </el-form-item>

      <el-form-item label="确认密码" prop="checkPass">
        <el-input
            type="password"
            v-model="ruleForm.checkPass"
            clearable
            size="default"
            prefix-icon="el-icon-refresh-right"
            placeholder="请输入密码"
            show-password
            autocomplete="off">
        </el-input>
      </el-form-item>
    </el-form>
    <div style="margin: 30px 0 0 0; text-align: center">
      <el-button type="primary" size="default" autocomplete="off" @click="submit">注册</el-button>
    </div>
  </div>

</template>

<script>
export default {
  name: "register",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: "",
        password: '',
        checkPass: '',
      },

      checked: false,
      rules: {
        username: [
          {required: true, message: "请输入用户名", trigger: "blur"},
          {min: 5, max: 10, message: "长度在 5 到 10 个字符之间", trigger: "blur"},
        ],
        password: [
          {validator: validatePass, trigger: 'blur'}
        ],
        checkPass: [
          {validator: validatePass2, trigger: 'blur'}
        ],
      },
    };
  },
  methods: {
    submit() {
      this.$router.push("/")
      /*this.$refs.ruleForm.validate((valid) => {
        if (valid) {  //表单校验合法
          this.rq.post("/user/register", this.ruleForm).then(res => {
            if (res.code === '200') {
              this.$message.success("注册成功")
              this.$router.push("/")
            } else {
              this.$message.error(res.msg)
            }
          }).catch((err) => {
            this.$message.error("未知错误")
          })
        }
      })*/
    }
    ,
  }
  ,
}
</script>

<style>

</style>
